# -*- coding: utf-8 -*-

from biblioteka.models import *

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.db.models import Q
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from session_messages import create_message

import datetime
import string

def calyKatalog(request, template_name='biblioteka/index.html'):	
	katalog = Ksiazka.objects.all()
	paginator = Paginator(katalog, 10)
	try:
        	page = int(request.GET.get('page', '1'))
        except ValueError:
        	page = 1
	try:
        	katalog = paginator.page(page)
    	except (EmptyPage, InvalidPage):
        	katalog = paginator.page(paginator.num_pages)
	egz = Egzemplarz.objects.filter( Q( user = request.user)).order_by('dataWypozyczenia')
	return render_to_response(template_name, RequestContext(request, {'katalog':katalog, 'egz':egz }))

def egzemplarze(request, tytul, template_name='biblioteka/egzemplarze.html'):
	katalog = Egzemplarz.objects.filter(Q(ksiazka__slug = tytul))
	ksiazka = katalog[0].ksiazka
	if request.method=="POST":
		egz = Egzemplarz.objects.get(isbn = request.POST["isbn"])
		egz.user = request.user
		egz.dataWypozyczenia = datetime.datetime.now().date()
		egz.dataZwrotu = egz.dataWypozyczenia + datetime.timedelta(days = 30)
		egz.dostepny = False
		egz.save()
		create_message(request, "Wypożyczono pomyślnie")
		return HttpResponseRedirect(reverse("calyKatalog"))	
	egz = Egzemplarz.objects.filter( Q( user = request.user)).order_by('dataWypozyczenia')
	return render_to_response(template_name, RequestContext(request, {'katalog':katalog , 'ksiazka':ksiazka, 'egz':egz }))

def szukaj(request, template_name='biblioteka/index.html'):
	egz = Egzemplarz.objects.filter( Q( user = request.user)).order_by('dataWypozyczenia')
	katalog = []
	if request.method == 'POST':
		nazwisko=request.POST['nazwisko']
		tytul=request.POST['tytul']	
		if not nazwisko and not tytul:
			create_message(request, "Przynajmniej jedno pole musi zostac wypełnione")
			return HttpResponseRedirect(reverse("calyKatalog"))	
		tytul = string.split(tytul, " ")
		nazwisko = string.split(nazwisko , " ")
		for t in tytul:
			for n in nazwisko:
				ksiazki = Ksiazka.objects.filter(tytul__contains = t).filter(Q(autorNazwisko__contains = n) | Q(autorImie__contains = n))			
		for kniga in ksiazki:
			if not kniga in katalog:
				katalog.append(kniga)
		if not katalog:
			create_message(request, "Brak wyników wyszukiwania")
			return HttpResponseRedirect(reverse("calyKatalog"))	

		paginator = Paginator(katalog, 10)
		try:
        		page = int(request.GET.get('page', '1'))
        	except ValueError:
        		page = 1
		try:
        		katalog = paginator.page(page)
    		except (EmptyPage, InvalidPage):
        		katalog = paginator.page(paginator.num_pages)
		return render_to_response(template_name, RequestContext(request, {'katalog': katalog, 'egz':egz}))

	
	
	
	

	

